#!/usr/bin/env python
import urllib2, urllib, json, time, pickle

"""
Original form list({ping1},{ping2},...)

Converted form  list({probeID:{PingRes1,PingRes2,...},...})
"""


#Convert to form {PID1:[ping,ping,...], PID2:[ping,ping,...]}
def joinProbePings(rawMSMdata):
    ping = dict()
    for pingResult in rawMSMdata:
        prb_id = pingResult.pop("prb_id")
        if prb_id in ping.keys():
            ping[prb_id].append(pingResult)
        else:
            ping[prb_id] = [pingResult]
    return ping

#Download results
#-------------------------------------------------------------------------------------------------------------------------
#Download all parts of pings towards the bogon. They are part of MSMIDs:
#   1423429, 1423431, 1423433, 1423435, 1423437, 1423440, 1423442   |  185.1.1.1
#   1423430, 1423432, 1423434, 1423436, 1423438, 1423441, 1423443   |
#
#
#
result = list()
for i in [ 1423430, 1423432, 1423434, 1423436, 1423438, 1423441, 1423443]:
    response = urllib.urlopen("https://atlas.ripe.net/api/v1/measurement/"+str(i)+"/result/")
    jsondata = json.loads(response.read())
    ##Convert to python objects
    #(true,false,null) = (True,False,None)
    result.extend(eval(str(jsondata)))
    time.sleep(3)
result = joinProbePings(bogonResult)





#Write data to file
with open("84.205.83.1", 'wb') as f:
    pickle.dump(bogonPing, f)

